﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using My.Pattern.Builder;

namespace My.Common.Error
{
    public class ErrorBuilder : IErrorBuilder
    {
        private Dictionary<string, string> _errorDescription;

        public ErrorBuilder()
        {
            RegisterErrorDescription();
        }

        private void RegisterErrorDescription()
        {
            _errorDescription = new Dictionary<string, string>();
            var enumType = typeof(Error);
            var valaues = Enum.GetValues(typeof(Error));
            foreach (var item in valaues)
            {
                var memInfo = enumType.GetMember(item.ToString());
                if (memInfo != null && memInfo.Any())
                {
                    var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (attributes != null && attributes.Any())
                    {
                        var description = ((DescriptionAttribute)attributes[0]).Description;
                        _errorDescription.Add(item.ToString(), description);
                    }
                }
            }
        }

        public string GetDescriptionFromErrorID(string errorID)
        {
            if (_errorDescription.ContainsKey(errorID))
            {
                return _errorDescription[errorID];
            }

            return string.Empty;
        }
    }

    public enum Error
    {
        #region Generic Messages
        [Description("<Field name> is required.")]
        GEN_001,
        [Description("Duplicated <Field name> is not allowed. Please re-enter <Field name>.")]
        GEN_002,
        [Description("No results match search criteria!")]
        GEN_003,
        [Description("Please select <Grid Name> item to edit.")]
        GEN_004,
        [Description("Cannot edit <Grid Name> on multiple selections.")]
        GEN_005,
        [Description("Please select <Grid Name> item(s) to delete.")]
        GEN_006,
        [Description("Are you sure you want to delete selected item(s)?")]
        GEN_007,
        [Description("Selected item(s) cannot be deleted because they are referenced by the other data.")]
        GEN_008,
        [Description("Accept only positive numbers.")]
        GEN_009,
        [Description("Are you sure you want to remove selected item(s)?")]
        GEN_010,
        [Description("No item to export.")]
        GEN_011,
        [Description("<End date/time field name> must be greater than or equal <Start date/time field name>.")]
        GEN_012,
        [Description("You are not allowed to access this function. Please contact admin for more details.")]
        GEN_013,

        #endregion

        #region FS Specific Messages
        [Description("User Name or Password is invalid. Please re-enter.")]
        TMS_FS_001_001,
        [Description("The account does not exist in TMS system. Please contact admin for more information.")]
        TMS_FS_001_002,
        [Description("Your account is disabled. Please contact admin for more information.")]
        TMS_FS_001_003,
        [Description("Please select user to grant privilege.")]
        TMS_FS_002_001,
        [Description("Cannot grant privilege on multiple user account selections.")]
        TMS_FS_002_002,
        [Description("Please select user(s) to register account(s).")]
        TMS_FS_002_003,
        [Description("<User Account> already existed. Please select another user account(s) to register.")]
        TMS_FS_002_004,
        [Description("Low value should be less than High value.")]
        TMS_FS_004_001,
        [Description("Duplicated parameter is not allowed in the same Predefined Work Instruction.")]
        TMS_FS_004_002,
        [Description("The entered Work Instruction Number is invalid, please ask to pre-define for this number.")]
        TMS_FS_005_001,
        [Description("Technician ID does not exist. Please re-enter it.")]
        TMS_FS_005_002,
        [Description("The scanned Technician ID doesn't match with the pre-defined parameters! Please choose another Technician ID!")]
        TMS_FS_005_003,
        [Description("The scanned Technician ID(s) (<Technician ID>) does/do not match with the pre-defined parameters anymore! Please contact admin for more information or remove the invalid scanned Technician ID(s).")]
        TMS_FS_005_004,
        [Description("The scanned Technician ID is invalid because user status is not 'AFUL'. Please re-scan another Technician ID.")]
        TMS_FS_005_006,
        [Description("The scanned Technician ID(s) (<Technician ID> ) is/are invalid anymore because user status is not AFUL! Please contact admin for more information or remove the invalid scanned Technician ID(s).")]
        TMS_FS_005_007,
        [Description("The scanned Technician ID(s) (<Technician ID> ) does/do not exist anymore! Please contact admin for more information or remove the invalid scanned Technician ID(s).")]
        TMS_FS_005_008,
        [Description("Export to excel successfully.")]
        TMS_FS_007_001,

        #endregion

        #region UTCMP Specific message
        [Description("<First Exist> and <Second Exist> already existed. Please re-enter another.")]
        UTC_MP_BothExists,
        [Description("<Exist> already existed. Please re-enter another.")]
        UTC_MP_OneExist,
        #endregion


        #region Other Messages
        [Description("Update successfully.")]
        UPDATE_SUCCESSFULLY_MESSAGE,
        [Description("Delete successfully.")]
        DELETE_SUCCESSFULLY_MESSAGE,
        [Description("Nothing changes.")]
        NOTHING_CHANGE_MESSAGE,
        [Description("You are not allowed to access this function. Please contact admin for more details.")]
        NO_HAVE_PERMISSION,
        [Description("FSHA275")]
        SUPER_ADMIN,
        [Description("You are not allowed to access this function. Please contact admin for more details")]
        NO_PERMISSION_MESSAGE,
        [Description("Your account is disabled. Please contact admin for more information")]
        USER_DISABLE_MESSAGE,
        [Description("The account does not exist in TMS system. Please contact admin for more information")]
        NOT_REGIS_MESSAGE,
        [Description("CurrentUser")]
        CURRENT_USER_SESSION,
        [Description("Error when saving user(s).")]
        ERR_SAVE_USER,
        [Description("This information is not exist in database. Please refresh page to have latest data.")]
        COULD_NOT_FIND_DATA
        #endregion

    }
}
